//
//  BottomView.m
//  BaseView
//
//  Created by 隗杰 on 16/3/31.
//  Copyright © 2016年 baobao. All rights reserved.
//

#import "BottomView.h"
#import "Masonry.h"

@interface BottomView ()
@property (strong,nonatomic)UIButton *lastAudioBtn;
@property (strong,nonatomic)UIButton *nextAudioBtn;
@property (strong,nonatomic)UIButton *playAudioBtn;
@property (strong,nonatomic)UISlider *slider;
@property (strong,nonatomic)UIProgressView *progressView;
@property (strong,nonatomic)UILabel *timeLabel;
@property (strong,nonatomic)UIImageView *iconImageView;
@property (strong,nonatomic)UILabel *storyNameLabel;
@property (strong,nonatomic)UILabel *authorNameLabel;
@property (strong,nonatomic)UIButton *listBtn;

@end

@implementation BottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect ScreenBounds = [UIScreen mainScreen].bounds;
        self.frame = CGRectMake(0, ScreenBounds.size.height - 44, ScreenBounds.size.width, 44);
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        [self setupUI];
    }
    return self;
}
#pragma mark - 单例
+ (instancetype)sharedBottomView {
    static dispatch_once_t onceToken;
    static BottomView *bottom;
    dispatch_once(&onceToken, ^{
        
        bottom = [[BottomView alloc] init];
        
    });
    return bottom;
}
#pragma mark - 设置 UI
- (void)setupUI {
    // 添加视图
    [self addSubview:self.lastAudioBtn];
    [self addSubview:self.nextAudioBtn];
    [self addSubview:self.playAudioBtn];
    [self addSubview:self.progressView];
    [self addSubview:self.slider];
    [self addSubview:self.timeLabel];
    
    // 布局
    [self.lastAudioBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(8);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.playAudioBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lastAudioBtn.mas_right).offset(8);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.nextAudioBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.playAudioBtn.mas_right).offset(8);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-8);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nextAudioBtn.mas_right).offset(8);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.right.mas_equalTo(self.timeLabel.mas_left).offset(-8);
        make.height.mas_equalTo(10);
    }];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nextAudioBtn.mas_right).offset(8);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.right.mas_equalTo(self.timeLabel.mas_left).offset(-8);
        make.height.mas_equalTo(1);
    }];
    
    // 绑定事件
    [self.playAudioBtn addTarget:self action:@selector(stopButnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.lastAudioBtn addTarget:self action:@selector(lastMusic:) forControlEvents:UIControlEventTouchUpInside];
    [self.nextAudioBtn addTarget:self action:@selector(nextMusic:) forControlEvents:UIControlEventTouchUpInside];
    [self.slider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark - 控件事件
// 暂停或者播放按钮
- (void)stopButnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if ([self.delegate respondsToSelector:@selector(bottomViewDidPlayOrStop:butnSelected:)]) {
        [self.delegate bottomViewDidPlayOrStop:self butnSelected:sender.selected];
    }
}

- (void)sliderClick:(UISlider *)sender {
    
    if ([self.delegate respondsToSelector:@selector(bottomViewSliderViewDidChangeValue:senderValue:)]) {
        [self.delegate bottomViewSliderViewDidChangeValue:self senderValue:sender.value];
    }
    
}
// 上一首啊
- (void)lastMusic:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(bottomViewDidClickLastAudioBtn:)]) {
        [self.delegate bottomViewDidClickLastAudioBtn:self];
    }
    
}

// 下一首啊
- (void)nextMusic:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(bottomViewDidClickNextAudioBtn:)]) {
        [self.delegate bottomViewDidClickNextAudioBtn:self];
    }
}
// 计算时间
- (void)customVideoSlider:(CMTime)duration {
    self.slider.maximumValue = CMTimeGetSeconds(duration);
    UIGraphicsBeginImageContextWithOptions((CGSize){ 1, 1 }, NO, 0.0f);
    UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.slider setMinimumTrackImage:transparentImage forState:UIControlStateNormal];
    [self.slider setMaximumTrackImage:transparentImage forState:UIControlStateNormal];
}

#pragma mark - 懒加载视图
- (UIButton *)lastAudioBtn {
    if (_lastAudioBtn == nil) {
        _lastAudioBtn = [[UIButton alloc] init];
        [_lastAudioBtn setTitle:@"上" forState: UIControlStateNormal];
        [_lastAudioBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_lastAudioBtn sizeToFit];
    }
    return _lastAudioBtn;
}
- (UIButton *)nextAudioBtn {
    if (_nextAudioBtn == nil) {
        _nextAudioBtn = [[UIButton alloc] init];
        [_nextAudioBtn setTitle:@"下" forState: UIControlStateNormal];
        [_nextAudioBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_nextAudioBtn sizeToFit];
    }
    return _nextAudioBtn;
}
- (UIButton *)playAudioBtn {
    if (_playAudioBtn == nil) {
        _playAudioBtn = [[UIButton alloc] init];
        [_playAudioBtn setTitle:@"停" forState: UIControlStateNormal];
        [_playAudioBtn setTitle:@"播" forState:  UIControlStateSelected];
        [_playAudioBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_playAudioBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [_playAudioBtn sizeToFit];
    }
    return _playAudioBtn;
}
- (UISlider *)slider {
    if (_slider == nil) {
        _slider = [[UISlider alloc] init];
        [_slider sizeToFit];
    }
    return _slider;
}
- (UIProgressView *)progressView {
    if (_progressView == nil) {
        _progressView = [[UIProgressView alloc] init];
        [_progressView sizeToFit];
    }
    return _progressView;
}
- (UILabel *)timeLabel {
    if (_timeLabel == nil) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.text = @"00:00/00:00";
        [_timeLabel sizeToFit];
    }
    return _timeLabel;
}
- (UIImageView *)iconImageView {
    if (_iconImageView == nil) {
        _iconImageView = [[UIImageView alloc] init];
    }
    return _iconImageView;
}
- (UILabel *)storyNameLabel {
    if (_storyNameLabel == nil) {
        _storyNameLabel = [[UILabel alloc] init];
    }
    return _storyNameLabel;
}
- (UILabel *)authorNameLabel {
    if (_authorNameLabel == nil) {
        _authorNameLabel = [[UILabel alloc] init];
    }
    return _authorNameLabel;
}
- (UIButton *)listBtn {
    if (_listBtn == nil) {
        _listBtn = [[UIButton alloc] init];
    }
    return _listBtn;
}

#pragma mark - setter 方法
- (void)setDuration:(CMTime)duration {
    _duration = duration;
    [self customVideoSlider:duration];
}
- (void)setProgress:(float)progress {
    _progress = progress;
    [self.progressView setProgress:progress animated:YES];
}
- (void)setCurrentSecond:(float)currentSecond {
    _currentSecond = currentSecond;
    [self.slider setValue:currentSecond animated:YES];
}
- (void)setTimeText:(NSString *)timeText {
    _timeText = timeText;
    self.timeLabel.text = timeText;
}

@end
